﻿using System.Windows;
using WindowsPhoneAudioStreaming.Audio.Streaming;
using System.Linq;

namespace WindowsPhoneAudioStreaming.SampleApplication.Logic
{
    public class ConfigurationLoader
    {
        /// <summary>
        /// Load the current configuration
        /// </summary>
        /// <returns></returns>
        public static ConfigurationSetup LoadConfiguration()
        {
            ConfigurationSetup currentSetup = new ConfigurationSetup();
            currentSetup = Application.Current.Resources["Configuration"] as ConfigurationSetup;
            
            var streamingPlaylist = currentSetup.Streams.Select(s =>
                                    new Audio.Streaming.PlaylistTrack()
                                    {
                                        Source = s.StreamUri,
                                        Title = s.Title,
                                        Artist = s.Description,
                                        AlbumArtUrl = s.ImageUri,
                                        IsStreaming = true
                                    });

            currentSetup.Playlist = streamingPlaylist.ToList();
            return currentSetup;
        }

        /// <summary>
        /// Save the current playlist to the configuration
        /// </summary>
        /// <param name="currentSetup"></param>
        public static void SavePlaylist(ConfigurationSetup currentSetup)
        {
            // Save it to isolated storage
            Audio.Streaming.Playlist playlist = new Audio.Streaming.Playlist();
            foreach (var playlistTrack in currentSetup.Playlist)
            {
                playlist.Tracks.Add(playlistTrack);
            }

            //Save so that is accessible by the streaming agent
            playlist.Save("playlist.xml");
        }

    }
}
